/*
    file:       mydefs.h

    purpose:    some helpful definitions for the project

    revisions
    000     RLL     2011-02-25  imported the file

*/

/*     Several helpful definitions      */
/*     from: Peter Dannegger            */

#ifndef _mydefs_h_
#define _mydefs_h_
#include<avr/io.h>

// ---------------------------------------------------------

// Access bits like variables
struct bits {
  uint8_t b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
} __attribute__((__packed__));

#define SBIT_(port,pin) ((*(volatile struct bits*)&port).b##pin)

#define	SBIT(x,y)       SBIT_(x,y)


// avoid push in main
int main( void )        __attribute__((OS_main));


// force access of interrupt variables
#define IVAR(x)         (*(volatile typeof(x)*)&(x))


// always inline function x
#define AIL(x)          static x __attribute__ ((always_inline)); static x


// NOP
#define nop()           __asm__ volatile("nop"::)

// ---------------------------------------------------------

// added my stuff

// macros for bit work

#define bit_get(ADDRESS,BIT)   (ADDRESS & (1<<BIT))

#define bit_set(ADDRESS,BIT)   (ADDRESS |= (1<<BIT))

#define bit_clear(ADDRESS,BIT)   (ADDRESS &= ~(1<<BIT))

#define bit_flip(ADDRESS,BIT)   (ADDRESS ^= (1<<BIT))

#define bit_write(c,p,m) (c ? bit_set(p,m) : bit_clear(p,m))

#define BIT(x) (0x01 << (x))

#define LONGBIT(x) ((unsigned long)0x00000001 << (x))

// ---------------------------------------------------------

#define ENABLE_INTERRUPTS       asm("sei")
#define DISABLE_INTERRUPTS      asm("cli")

// ---------------------------------------------------------

#define OFF  1
#define ON   0

#endif
